package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.Utils.POIUtils;
import com.itheima.common.MessageConst;
import com.itheima.entity.Result;
import com.itheima.exception.BusinessRuntimeException;
import com.itheima.service.MemberService;
import com.itheima.service.ReportService;
import com.itheima.service.SetmealService;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @ClassName ReportController
 * @Description: TODO
 * @Author 郑乾
 * @Date 2020-07-04
 * @Version V1.0
 **/
@RestController
@RequestMapping("/report")
@Slf4j
public class ReportController {
    @Reference
    MemberService memberService;
    @Reference
    SetmealService setmealService;
    @Reference
    ReportService reportService;

    @RequestMapping("getMemberReport")
    public Result getMemberReport() {
        //获取会员统计信息
        Map<String, Object> map = memberService.getMemberReport();
        return new Result(true, MessageConst.GET_MEMBER_NUMBER_REPORT_SUCCESS, map);
    }

    @RequestMapping("getSetmealReport")
    public Result getSetmealReport() {
        //获取套餐统计信息
        List<String> setmealNames = new ArrayList<>();
        //创建套餐名称和预约数量的集合
        List<Map> setmealCount = setmealService.getSetmealReport();
        if (setmealCount != null && setmealCount.size() > 0) {
            for (Map map : setmealCount) {
                String name = (String) map.get("name");
                setmealNames.add(name);
            }
        }
        Map<String, Object> map = new HashMap<>();
        map.put("setmealNames", setmealNames);
        map.put("setmealCount", setmealCount);
        log.debug("结果:..." + map.toString());
        return new Result(true, MessageConst.GET_SETMEAL_COUNT_REPORT_SUCCESS, map);
    }

    @RequestMapping("getBusinessReportData")
    public Result getBusinessReportData() {
        //运营数据统计
        Map<String, Object> result = reportService.getBussinessReport();
        log.debug("result..." + result);
        return new Result(true, MessageConst.ACTION_SUCCESS, result);
    }

    @RequestMapping("exportBusinessReport")
    public void exportBusinessReport(HttpServletResponse response) {
        try {
            Map<String, Object> bussinessReport = reportService.getBussinessReport();
            Object reportDate = bussinessReport.get("reportDate");
            Object totalMember = bussinessReport.get("totalMember");
            Object todayNewMember = bussinessReport.get("todayNewMember");
            Object thisWeekNewMember = bussinessReport.get("thisWeekNewMember");
            Object thisMonthNewMember = bussinessReport.get("thisMonthNewMember");
            Object todayOrderNumber = bussinessReport.get("todayOrderNumber");
            Object todayVisitsNumber = bussinessReport.get("todayVisitsNumber");
            Object thisWeekOrderNumber = bussinessReport.get("thisWeekOrderNumber");
            Object thisWeekVisitsNumber = bussinessReport.get("thisWeekVisitsNumber");
            Object thisMonthOrderNumber = bussinessReport.get("thisMonthOrderNumber");
            Object thisMonthVisitsNumber = bussinessReport.get("thisMonthVisitsNumber");
            List<Map<String, Object>> hotSetmeal = (List<Map<String, Object>>) bussinessReport.get("hotSetmeal");
            //创建模板对象
            InputStream inputStream = this.getClass().getResourceAsStream("/template/report_template.xlsx");
            XSSFWorkbook workbook = new XSSFWorkbook(inputStream);
            //将数据传入模板
            //获取工作表对象 设置统计日期
            XSSFSheet sheet = workbook.getSheet("Sheet1");
            POIUtils.writeCell(sheet, 2, 5, reportDate);
            //将数据录入
            POIUtils.writeCell(sheet, 4, 5, todayNewMember);
            POIUtils.writeCell(sheet, 4, 7, totalMember);
            POIUtils.writeCell(sheet, 5, 5, thisWeekNewMember);
            POIUtils.writeCell(sheet, 5, 7, thisMonthNewMember);
            POIUtils.writeCell(sheet, 7, 5, todayOrderNumber);
            POIUtils.writeCell(sheet, 7, 7, todayVisitsNumber);
            POIUtils.writeCell(sheet, 8, 5, thisWeekOrderNumber);
            POIUtils.writeCell(sheet, 8, 7, thisWeekVisitsNumber);
            POIUtils.writeCell(sheet, 9, 5, thisMonthOrderNumber);
            POIUtils.writeCell(sheet, 9, 7, thisMonthVisitsNumber);
            //声明行号
            int rownum = 12;
            for (Map<String, Object> map : hotSetmeal) {
                POIUtils.writeCell(sheet, rownum, 4, map.get("name"));
                POIUtils.writeCell(sheet, rownum, 5, map.get("setmeal_count"));
                POIUtils.writeCell(sheet, rownum, 6, map.get("proportion"));
                rownum++;
            }
            //下载excel到本地
            ServletOutputStream out = response.getOutputStream();
            response.setContentType("application/vnd.ms-excel");
            response.setHeader("content-Disposition", "attachment;filename=" + reportDate + "_report.xlsx");
            //输出流下载
            workbook.write(out);
            out.flush();
            out.close();
            workbook.close();
        } catch (IOException e) {
            e.printStackTrace();
            throw new BusinessRuntimeException(MessageConst.GET_BUSINESS_REPORT_FAIL);
        }
    }
}
